/* ScrollViewExtras.m:
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * A generic scrollview driver object to display/print text 
 * Written by Jack Greenfield
 *
 */

#import	<objc/objc-runtime.h>
#import "ScrollViewExtras.h"

@implementation ScrollView(ScrollViewExtras)

- sprintf:(const char *)format, ...
{
    int length;
    static char buffer[65536];
    va_list arguments;

    if ([window isVisible])
    {
	va_start(arguments, format);
	vsprintf(buffer, format, arguments);
	va_end(arguments);
    
	length = [[self docView] textLength];
	[[self docView] setSel:length :length];
	[[self docView] replaceSel:buffer];
	[[self docView] scrollSelToVisible];
    }

    return self;
}

- vsprintf:(const char *)format arguments:(va_list)arguments
{
    int length;
    static char buffer[65536];

    if ([window isVisible])
    {
	vsprintf(buffer, format, arguments);
	length = [[self docView] textLength];
	[[self docView] setSel:length :length];
	[[self docView] replaceSel:buffer];
	[[self docView] scrollSelToVisible];
    }

    return self;
}

- clear:sender
{
    int length;

    if ([window isVisible])
    {
	length = [[self docView] textLength];
	[[self docView] setSel:0 :length];
	[[self docView] replaceSel:""];
	[[self docView] scrollSelToVisible];
    }

    return self;
}

- print:sender
{
    if ([window isVisible])
	[[self docView] printPSCode:sender];

    return self;
}

- printFrom:sender
{
    return [self sprintf:"%s\n", [sender stringValue]];
}

@end
